package com.colorlight.cloud.gateway.api.message.utils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Demon
 * @date 20/12/2023  下午4:57
 */
public  class BaseType {
	
	public static Class<?> convert(Class<?> aClass) {
		return TypeMapping.getOrDefault(aClass, aClass);
	}
	
	public static Boolean isContains(Class<?> aClass) {
		return TypeMapping.WRAPPER_TO_PRIMITIVE_MAP.containsKey(aClass);
	}
	
	private enum TypeMapping {
		/** 基本类型 */
		INTEGER(Integer.class, int.class),
		LONG(Long.class, long.class),
		FLOAT(Float.class, float.class),
		DOUBLE(Double.class, double.class),
		BYTE(Byte.class, byte.class),
		SHORT(Short.class, short.class),
		CHARACTER(Character.class, char.class),
		BOOLEAN(Boolean.class, boolean.class),
		STRING(String.class, String.class);
		/** 封装类型 */
		private final Class<?> wrapperType;
		
		private final Class<?> primitiveType;
		
		TypeMapping(Class<?> wrapperType, Class<?> primitiveType) {
			this.wrapperType = wrapperType;
			this.primitiveType = primitiveType;
		}
		
		private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_MAP;
		
		static {
			WRAPPER_TO_PRIMITIVE_MAP =
					Arrays.stream(values())
							.collect(
									Collectors.toMap(
											TypeMapping::getWrapperType,
											TypeMapping::getPrimitiveType));
		}
		
		public static Class<?> getOrDefault(Class<?> clazz, Class<?> defaultValue) {
			return WRAPPER_TO_PRIMITIVE_MAP.getOrDefault(clazz, defaultValue);
		}
		
		public Class<?> getWrapperType() {
			return wrapperType;
		}
		
		public Class<?> getPrimitiveType() {
			return primitiveType;
		}
	}
}